<?php session_start(); ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<?php
if (isset($_SESSION['lang']))
    require_once "./language_files/viewmessages_" . $_SESSION['lang'] . ".php";
else
    require_once "./language_files/viewmessages_en.php";
?>
<html>
    <head>
        <meta http-equiv="X-UA-Compatible" content="IE=9" />
        <link href="style3.css" rel="stylesheet" type="text/css" />
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
        <style type="text/css">

            #loading {
                text-align:left;
            }
            #container .pagination ul li.inactive,
            #container .pagination ul li.inactive:hover{
                background-color:#ededed;
                color:#bababa;
                border:1px solid #bababa;
                cursor: default;
            }
            #container .data ul li{
                list-style: none;
                font-family: verdana;
                margin: 5px 0 5px 0;
                color: #000;
                font-size: 13px;
            }

            #container .pagination{
                width: 99%;
                height: 25px;
            }
            #container .pagination ul li{
                list-style: none;
                float: left;
                border: 1px solid #003366;
                padding: 3px 6px 3px 6px;
                margin: 3px 3px 3px 3px;
                font-family: arial;
                font-size: 14px;
                color: #003366;
                font-weight: bold;
                background-color: #f2f2f2;
            }
            #container .pagination ul li:hover{
                color: #fff;
                background-color: #003366;
                cursor: pointer;
            }

            .total
            {
                float:right;font-family:arial;color:#999;
            }
           

        </style>

    </head>
    <body>

        <?php
        include 'functions.php';
// Connect to the database
        $member_id = $_SESSION['member_id'];
        //query to retrieve all the oncferences where the user is the creator
        // Print the top of a table

        echo '<form id="functions"><fieldset>
            <input type="button" value="' . $lang['create'] . '" name="submit" class="padding" id="newM"/><br />
<button class="disabledButton" disabled="true" name="deleteSelected" id="deleteSelected" type="submit">Delete selected</button>
<button class="disabledButton" name="mark" disabled="true" type="submit" id="mark">Mark read/unread</button>
<input type="hidden" value="" name="Bname" id="Bname" />
</fieldset>';
        echo "<div id='container'></div>";   
        ?>
    </form>
        <?php include "jquery.php" ?>
    <script type="text/javascript" src="functions.js"></script>
    <script type="text/javascript">
        //this part of the code is for hiding and showing the messages table
        $(document).ready(function(){
            check_top();
            $("#container").html("<p align='center'><img src='main-loader.gif'/></p>").fadeIn('fast');
            loadData(1);
            function loading_show(){
                $('#loading').html("<img src='menu icons/circular.gif'/>").fadeIn('fast');
            }
            function loading_hide(){
                $('#loading').fadeOut('fast');
            }                
            function loadData(page){
                loading_show();
                $.ajax
                ({
                    type: "POST",
                    url: "load_data.php",
                    data: "page="+page,
                    success: function(msg)
                    {
                        $("#container").ajaxComplete(function(event, request, settings)
                        {
                            loading_hide();
                            $("#container").html(msg);
                            resize_iframe();
                        });
                    }
                });
            }
            
            $('#container .pagination li.active').live('click',function(){
                var page = $(this).attr('p');
                loadData(page);
                    
            });           
            $('.goto').live('keyup',function(){
                var page = parseInt($(this).val());
                var no_of_pages = parseInt($('.total').attr('a'));
                if(page != 0 && page <= no_of_pages){
                    loadData(page);
                }else{
                    alert('Enter a page between 1 and '+no_of_pages);
                    $(this).val("").focus();
                    return false;
                }
                    
            });
            labelM = top.document.getElementById("num_msg");
            $.ajax ({
                type: 'POST',
                url:  'seen_messages.php',
                success: function(response) {
                    if(response == "0") {
                        $(labelM).html('');
                        $(labelM).removeClass("num_msg");
                    } else {
                        $(labelM).html(response);
                        $(labelM).addClass("num_msg");
                    }
                }
            });
            $("#newM").click (function(){
                frame = top.document.getElementById("collabsoft");
                $(frame).attr("src", 'choosepeople.php');
            });
            $("#mark, #deleteSelected").click(function(){
                $("#Bname").val($(this).attr("name"));
            });
            $("#functions").bind("submit", function(e){
                e.preventDefault();
                if($("#Bname").val() == "mark") {
                    var alrt = confirm("Are you sure you want to change status of selected messages?\nNote that read messages will be marked as unread and vise versa.")
                } else {
                    var alrt = confirm("Are you sure you want to delete selected messages?\nNote that you can not restore them back.");
                }
                if(alrt == true) {
                    $.ajax({
                        data: $(this).serialize(),
                        type: "POST",
                        url: "messageFunctions.php",
                        success: function(response) {
                            location.reload();
                        }
                    });
                }
            });
            $(document).on("click","#container input[type=checkbox]", function(){
                if($("#container :checked").length > 0) {
                    $("#deleteSelected").removeAttr("disabled");
                    $("#deleteSelected").removeClass("disabledButton");
                    $("#mark").removeAttr("disabled");
                    $("#mark").removeClass("disabledButton");
                } else {
                    $("#deleteSelected").attr("disabled", true);
                    $("#deleteSelected").addClass("disabledButton");
                    $("#mark").attr("disabled", true);
                    $("#mark").addClass("disabledButton");
                }
            });
            $(document).on("click","#all", function(){
                if($("#container input[type=checkbox]").attr("checked")) {
                    $("#deleteSelected").removeAttr("disabled");
                    $("#deleteSelected").removeClass("disabledButton");
                    $("#mark").removeAttr("disabled");
                    $("#mark").removeClass("disabledButton");
                    $("#container input[type=checkbox]").attr('checked',true);
                    $("#container label").html("Un-check all");
                } else {
                    $("#deleteSelected").attr("disabled", true);
                    $("#deleteSelected").addClass("disabledButton");
                    $("#mark").attr("disabled", true);
                    $("#mark").addClass("disabledButton");
                    $("#container input[type=checkbox]").attr('checked',false);
                    $("#container label").html("Check all");
                }
            });
            $(document).on("click",".sub", function(){
                labelM = top.document.getElementById("num_msg");
                frame = top.document.getElementById("collabsoft");
                $(this).closest("table").addClass("seen_message");
                $(this).closest("table").removeClass("unread_message");
                mesg_id = $(this).attr('msg_id');
                id = $(this).attr('mID');
                link = $(this).attr("link");
                $.ajax ({
                    data: {message_id:mesg_id, member_id:id},
                    type: 'POST',
                    url:  'seen_messages.php',
                    success: function(response) {
                        if(response == "(0)") {
                            $(labelM).html('');
                        } else {
                            $(labelM).html(response);
                        }

                    }

                });
                $(frame).attr("src", link);
            });
            $(document).on("click",".delete", function(){
                var conf = confirm("Are you sure you want to delete this message?");
                if(conf == true) {
                    mesg_id = $(this).attr('msg_id');
                    $.ajax ({
                        data: {message_id:mesg_id},
                        type: 'POST',
                        url:  'deletM.php',
                        success: function(response) {
                            if(response != "message deleted successfully") {
                                alert(response);
                            }
                            labelM=top.document.getElementById("num_msg");
                            mesg_id = $(this).attr('msg_id');
                            id = $(this).attr('mID');
                            $.ajax ({
                                data: {message_id:mesg_id, member_id:id},
                                type: 'POST',
                                url:  'seen_messages.php',
                                success: function(response) {

                                    if(response == "(0)") {
                                        $(labelM).html('');
                                    } else {
                                        $(labelM).html(response);
                                    }
                                }
                            });
                            location.reload();
                        }
                    });
                }
            });
        });
    </script>
</body>
</html>